function Master_hom_FEdemoshrt_grID_wtp_tomlab_varagin_j_bs(subsample, inc, set_seed, j, index_bs)

tic

global state_i week_i J T S MT MTvector K WB...
price_num rebate_estar_num kwh_num elec_num estar_num ...
price_denom rebate_estar_denom rebate_denom kwh_denom elec_denom estar_denom ...
bchoice bchoice_s bchoice_ij id_i id_j id_ij id_m id_n  demo pidnest choicenest_ij brandweek_denom brandweek_num prod_denom prod_num...
type_id2_3_demo1_num type_id2_3_demo1_denom type_id3_demo1_num type_id3_demo1_denom AV_demo1_num AV_demo1_denom  ice_sc_demo1_num ice_sc_demo1_denom...
type_id2_3_demo2_num type_id2_3_demo2_denom type_id3_demo2_num type_id3_demo2_denom AV_demo2_num AV_demo2_denom  ice_sc_demo2_num ice_sc_demo2_denom...
type_id2_3_demo3_num type_id2_3_demo3_denom type_id3_demo3_num type_id3_demo3_denom AV_demo3_num AV_demo3_denom  ice_sc_demo3_num ice_sc_demo3_denom...
type_id2_3_demo4_num type_id2_3_demo4_denom type_id3_demo4_num type_id3_demo4_denom AV_demo4_num AV_demo4_denom  ice_sc_demo4_num ice_sc_demo4_denom...
type_id2_3_demo5_num type_id2_3_demo5_denom type_id3_demo5_num type_id3_demo5_denom AV_demo5_num AV_demo5_denom  ice_sc_demo5_num ice_sc_demo5_denom...
type_id2_3_demo6_num type_id2_3_demo6_denom type_id3_demo6_num type_id3_demo6_denom AV_demo6_num AV_demo6_denom  ice_sc_demo6_num ice_sc_demo6_denom;


global id;
global LLC_5_18; 
  
%Create Diary
date_now = clock;
date_now = strcat(num2str(date_now(1)),'_',num2str(date_now(2)),'_', num2str(date_now(3)), num2str(date_now(4)), num2str(date_now(5)));
diary(strcat('\\c3\rdat\SHoude\Research/sears/estar_results/struct_est/','log_hom_FEdemoshrt_',num2str(subsample),'_inc',num2str(inc),'_sd',num2str(set_seed),'_04132018_bs_',num2str(j),'.log'));

%Create Output: See below and make changes
results_file1=strcat('\\c3\rdat\SHoude\Research/sears/estar_results/struct_est/','hom_FEdemoshrt_wtp_LLC_5_18_',num2str(subsample),'_inc',num2str(inc),'_sd',num2str(set_seed),'_04132018_bs_',num2str(j),'.mat');

%Read Files
	tmp_file1=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\choiceset_identifier_trimester_week_zipcode_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    id=load(tmp_file1);
    id=id(index_bs,:);
    id(:,1:7)=[];
    choicest_size=sum(id,2);
    %id_discard=find(choicest_size>200 | choicest_size<50);
    id_discard=find(choicest_size>400);
    id(id_discard,:)=[];  
    choicest_size(id_discard)=[]; 
    id=id';
    [id_i,id_j,id_s]=find(id);
    id_ij=find(id);
    [id_m,id_n] = size(id);
    
    
 	%bchoice=load('H:\Research\sears\estar_data\refrigerators\bchoice_046_2008_2012_v11022017_struct_52171_4.csv'); 
    tmp_file2=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\bchoice_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
 	bchoice=load(tmp_file2);
 	bchoice=bchoice(index_bs,:); 
    bchoice(:,1)=[];
    bchoice(id_discard,:)=[]; 
    bchoice=bchoice';
    [bchoice_i,bchoice_j,bchoice_s]=find(bchoice);
    bchoice_ij=find(bchoice);
    [bchoice_m,bchoice_n] = size(bchoice);  
    
    bchoiceIJ=bchoice(id_ij);
    MT=size(id,2);
    MTvector=ones(MT,1);
    J=size(id,1);
    
    tmp_file3=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\estar_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    estar=load(tmp_file3); 
    estar=estar(index_bs,:);
    estar(:,1:7)=[];
    estar(id_discard,:)=[]; 
    estar=estar';
    estar_num=estar(bchoice_ij);
    estar_denom=estar(id_ij);
    clear estar;
    
    tmp_file4=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\rebate_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    rebate=load(tmp_file4); 
    rebate=rebate(index_bs,:); 
    rebate(:,1:7)=[];
    rebate=rebate/1000;
    rebate_J=repmat(rebate,1,J);
    rebate_J(id_discard,:)=[]; 
    rebate_J=rebate_J';
    rebate_denom=rebate_J(id_ij);
    rebate_estar_num=rebate_J(bchoice_ij).*estar_num;
    rebate_estar_denom=rebate_J(id_ij).*estar_denom;
    clear rebate rebate_J;
    
    tmp_file5=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\tax_rate_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    tax=load(tmp_file5); 
    tax=tax(index_bs,:);
    tax(:,1:7)=[];
    tax(id_discard,:)=[]; 
    taxp=1+repmat(tax',J,1);
    
    tmp_file6=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\price_mean_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    price_net=load(tmp_file6); 
    price_net=price_net(index_bs,:);
    price_net(:,1:7)=[];
    price_net(id_discard,:)=[]; 
    price_net=price_net';
    price=taxp.*price_net;
    price=price/1000;
    price_num=price(bchoice_ij);
    price_denom=price(id_ij);
    clear price_net taxp tax;
    
    tmp_file7=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\kwh_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    kwh=load(tmp_file7); 
    kwh=kwh(index_bs,:);
    kwh(:,1:7)=[];
    kwh(id_discard,:)=[]; 
    kwh=kwh';
    kwh=kwh/1000;
    kwh_num=kwh(bchoice_ij);
    kwh_denom=kwh(id_ij);
    clear kwh;
    
    tmp_file8=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\elec_county_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    elec=load(tmp_file8); 
    elec=elec(index_bs,:);
    elec(:,1:7)=[];
    elec_J=repmat(elec,1,J);
    elec_J(id_discard,:)=[]; 
    elec_J=elec_J';
    elec_num=elec_J(bchoice_ij).*kwh_num;
    elec_denom=elec_J(id_ij).*kwh_denom;
    clear elec_J;
    
    tmp_file9=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\demo_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    demo=load(tmp_file9); 
    demo=demo(index_bs,:);

        %hd_id income2 income3 education2 education3 fam_size age political2 political3
    demo(:,1)=[];
    demo(:,1:2)=[];
    demo(:,3)=demo(:,3)/10;
    demo(:,4)=demo(:,4)/100;
    demo1_J=repmat(demo(:,1),1,J); %education2
    demo2_J=repmat(demo(:,2),1,J); %education3
    demo3_J=repmat(demo(:,3),1,J); %fam_size
    demo4_J=repmat(demo(:,4),1,J); %age
    demo5_J=repmat(demo(:,5),1,J); %political2
    demo6_J=repmat(demo(:,6),1,J); %political3
   
%Create Interactions Attributes-Demographics     
	tmp_file10=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\type_id_2_3_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
	type_id2_3=load(tmp_file10); 
	type_id2_3=type_id2_3(index_bs,:);
    type_id2_3(:,1:7)=[];
   
    type_id2_3_demo1=type_id2_3.*demo1_J;
    type_id2_3_demo1(id_discard,:)=[]; 
    type_id2_3_demo1=type_id2_3_demo1';
    type_id2_3_demo1_num=type_id2_3_demo1(bchoice_ij);
    type_id2_3_demo1_denom=type_id2_3_demo1(id_ij);
    clear type_id2_3_demo1;
    
    type_id2_3_demo2=type_id2_3.*demo2_J;
    type_id2_3_demo2(id_discard,:)=[];
    type_id2_3_demo2=type_id2_3_demo2';
    type_id2_3_demo2_num=type_id2_3_demo2(bchoice_ij);
    type_id2_3_demo2_denom=type_id2_3_demo2(id_ij);
    clear type_id2_3_demo2;
    
    type_id2_3_demo3=type_id2_3.*demo3_J;
    type_id2_3_demo3(id_discard,:)=[];
    type_id2_3_demo3=type_id2_3_demo3';
    type_id2_3_demo3_num=type_id2_3_demo3(bchoice_ij);
    type_id2_3_demo3_denom=type_id2_3_demo3(id_ij);
    clear type_id2_3_demo3;
    
    type_id2_3_demo4=type_id2_3.*demo4_J;
    type_id2_3_demo4(id_discard,:)=[];
    type_id2_3_demo4=type_id2_3_demo4';
	type_id2_3_demo4_num=type_id2_3_demo4(bchoice_ij);
    type_id2_3_demo4_denom=type_id2_3_demo4(id_ij);
    clear type_id2_3_demo4;
    
    type_id2_3_demo5=type_id2_3.*demo5_J;
    type_id2_3_demo5(id_discard,:)=[];
    type_id2_3_demo5=type_id2_3_demo5';
    type_id2_3_demo5_num=type_id2_3_demo5(bchoice_ij);
    type_id2_3_demo5_denom=type_id2_3_demo5(id_ij);
    clear type_id2_3_demo5;
    
    type_id2_3_demo6=type_id2_3.*demo6_J;
    type_id2_3_demo6(id_discard,:)=[];
    type_id2_3_demo6=type_id2_3_demo6';
    type_id2_3_demo6_num=type_id2_3_demo6(bchoice_ij);
    type_id2_3_demo6_denom=type_id2_3_demo6(id_ij);
    clear type_id2_3_demo6;
   
   
    tmp_file11=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\AV_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
    AV=load(tmp_file11);
    AV=AV(index_bs,:); 
    AV(:,1:7)=[];
    AV=AV/100;
    
    AV_demo1=AV.*demo1_J;
    AV_demo1(id_discard,:)=[];
    AV_demo1=AV_demo1';
    AV_demo1_num=AV_demo1(bchoice_ij);
    AV_demo1_denom=AV_demo1(id_ij);
    clear AV_demo1;
    
    AV_demo2=AV.*demo2_J;
    AV_demo2(id_discard,:)=[];
    AV_demo2=AV_demo2';
    AV_demo2_num=AV_demo2(bchoice_ij);
    AV_demo2_denom=AV_demo2(id_ij);
    clear AV_demo2;
    
    AV_demo3=AV.*demo3_J;
    AV_demo3(id_discard,:)=[];
    AV_demo3=AV_demo3';
    AV_demo3_num=AV_demo3(bchoice_ij);
    AV_demo3_denom=AV_demo3(id_ij);
    clear AV_demo3;
    
    AV_demo4=AV.*demo4_J;
    AV_demo4(id_discard,:)=[];
    AV_demo4=AV_demo4';
    AV_demo4_num=AV_demo4(bchoice_ij);
    AV_demo4_denom=AV_demo4(id_ij);
    clear AV_demo4;
    
    AV_demo5=AV.*demo5_J;
    AV_demo5(id_discard,:)=[];
    AV_demo5=AV_demo5';
    AV_demo5_num=AV_demo5(bchoice_ij);
    AV_demo5_denom=AV_demo5(id_ij);
    clear AV_demo5;
    
    AV_demo6=AV.*demo6_J;
    AV_demo6(id_discard,:)=[];
    AV_demo6=AV_demo6';
    AV_demo6_num=AV_demo6(bchoice_ij);
    AV_demo6_denom=AV_demo6(id_ij);
    clear AV_demo6;
   
    clear AV;
    
    tmp_file12=strcat('\\c3\rdat\SHoude\Research/sears/estar_data\refrigerators\ice_sc_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
    ice_sc=load(tmp_file12); 
    ice_sc=ice_sc(index_bs,:); 
    ice_sc(:,1:7)=[];
   
    ice_sc_demo1=ice_sc.*demo1_J;
    ice_sc_demo1(id_discard,:)=[];
    ice_sc_demo1=ice_sc_demo1';
    ice_sc_demo1_num=ice_sc_demo1(bchoice_ij);
    ice_sc_demo1_denom=ice_sc_demo1(id_ij);
    clear ice_sc_demo1;
    
    ice_sc_demo2=ice_sc.*demo2_J;
    ice_sc_demo2(id_discard,:)=[];
    ice_sc_demo2=ice_sc_demo2';
    ice_sc_demo2_num=ice_sc_demo2(bchoice_ij);
    ice_sc_demo2_denom=ice_sc_demo2(id_ij);
    clear ice_sc_demo2;
    
    ice_sc_demo3=ice_sc.*demo3_J;
    ice_sc_demo3(id_discard,:)=[];
    ice_sc_demo3=ice_sc_demo3';
    ice_sc_demo3_num=ice_sc_demo3(bchoice_ij);
    ice_sc_demo3_denom=ice_sc_demo3(id_ij);
    clear ice_sc_demo3;
    
    ice_sc_demo4=ice_sc.*demo4_J;
    ice_sc_demo4(id_discard,:)=[];
    ice_sc_demo4=ice_sc_demo4';
    ice_sc_demo4_num=ice_sc_demo4(bchoice_ij);
    ice_sc_demo4_denom=ice_sc_demo4(id_ij);
    clear ice_sc_demo4;
    
    ice_sc_demo5=ice_sc.*demo5_J;
    ice_sc_demo5(id_discard,:)=[];
    ice_sc_demo5=ice_sc_demo5';
    ice_sc_demo5_num=ice_sc_demo5(bchoice_ij);
    ice_sc_demo5_denom=ice_sc_demo5(id_ij);
    clear ice_sc_demo5;
    
    ice_sc_demo6=ice_sc.*demo6_J;
    ice_sc_demo6(id_discard,:)=[];
    ice_sc_demo6=ice_sc_demo6';
    ice_sc_demo6_num=ice_sc_demo6(bchoice_ij);
    ice_sc_demo6_denom=ice_sc_demo6(id_ij);
    clear ice_sc_demo6;
   
    clear ice_sc;
    
    prod=repmat([1:J],MT,1)';
    prod_denom=prod(id_ij);
    prod_num=prod(bchoice_ij);
    
 clear data;   


 
%Initialize Parameters
  
    %Product FE
        alpha=ones(1,J-1);
   %Price Elasticity
        eta=-0.00524720859088;
    %Estar    
        tau= 0.186990388021096 ;
    %Rebate
        pi=0.0002;
    %Electricity   
        theta=-0.01652660097783;
 	
    %demo 
    	beta=0.01*ones(1,12);
        
  %Homogeneous With Product FEs: ChoiceProb_hom_FEnocol_vs
   %FE
   param_b(1:J-1,1)=alpha;
   %Behavioral param
   param_b(J,1)=eta*1000;
   param_b(J+1,1)=tau;
   param_b(J+2,1)=pi*1000;
   param_b(J+3,1)=theta*1000;
   %AttributesXdemo: 6-type_id2_3, 6-AV
   param_b(J+4:J+15,1)=beta;
  
  Nparam=J+15;
  
  rho_5=1/(1+0.05);
  LLC_5_18=rho_5*(1-rho_5^18)/(1-rho_5);
toc 
   

%Using Tomlab-Knitro
tic
	        Name = 'ML Knitro';
			
			x_L=-1000*ones(Nparam,1);
			x_U=1000*ones(Nparam,1);
			fLowBnd = -inf;    % Lower bound on function.
			
			tol_outer = 1.e-12;
			
			hess_pat=ones(Nparam);
		
			Prob = conAssign('ChoiceProb_hom_FEdemoshrt_WTP_tomlab','ML_grad_hom_FEdemoshrt_WTP_tomlab',[],hess_pat, x_L, x_U, Name, param_b,[], fLowBnd, [], [], [], [], [], [], [], [], []);
			
			    Prob.PriLevOpt = 3;
			    Prob.KNITRO.PrintFile = ''; 
			    Prob.KNITRO.options.MAXIT = 5000;    % Setting maximum number of iterations     
			    Prob.KNITRO.options.OPTTOL = tol_outer ;    % set outer-loop tolerance
			      
                Prob.KNITRO.options.PRESOLVE = 0; 
			    Prob.KNITRO.options.ALG = 1;
			    Prob.KNITRO.options.HESSOPT = 2;    % to be used along with first-order derivatives          
			    Knitro_Result = tomRun('knitro', Prob, 1);
			    theta_est1 = Knitro_Result.x_k
			    fvalue_Knitro = Knitro_Result.f_k;
			    INFO_Knitro = Knitro_Result.Inform;
                Hessian_Knitro=Knitro_Result.H_k;
                gradient_Knitro=Knitro_Result.g_k;
toc_knitro=toc
    
 eval(['save ' results_file1 ' Knitro_Result theta_est1 fvalue_Knitro toc_knitro Hessian_Knitro gradient_Knitro']);  
     

end 






      





